package org.tom.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tom.basic.exception.MyException;
import org.tom.basic.util.JsonResult;
import org.tom.basic.util.PageList;
import org.tom.org.domain.Shop;
import org.tom.org.query.ShopQuery;
import org.tom.org.service.IShopService;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;


    @ApiOperation(value = "店铺查询单个店铺",notes = "通过id进行查询")
    @GetMapping("{id}")  // get方法的请求，参数为id查询单条数据
    public Shop loadOne(@PathVariable("id") Long id){
        Shop shop = shopService.findById(id);
        return shop;
    }

    @ApiOperation(value = "查询所有的店铺",notes = "不需要任何参数")
    @GetMapping // 查询所有
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    @ApiOperation(value = "店铺删除",notes = "通过id进行删除")
    @DeleteMapping("{id}") // 删除携带参数id
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping // 修改和添加
    public JsonResult updateAndSave(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                shopService.insert(shop);
            }else {
                shopService.update(shop);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @ApiOperation(value = "分页显示",notes = "传输分页需要的当前页和每页显示的条数")
    @PostMapping // 分页接受分页数据用对象
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryList(shopQuery);
    }

    @ApiOperation(value = "批量删除店铺",notes = "传id集合")
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){ // 使用数组类型的集合接受参数
        try {
            shopService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @ApiOperation(value = "查询所有的店铺子店铺",notes = "不需要任何参数")
    @GetMapping("/tree") // 查询所有
    public List<Shop> findAllChildren(){
        return shopService.loadTree();
    }

    /* 店铺入驻 */
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){
        try {
            shopService.shopRegister(shop);
            return JsonResult.me();

        } catch (MyException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常");
        }
    }

}
